package com.shoulder.boot.process.service.impl;

import com.shoulder.core.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.process.dto.FormAddDTO;
import com.shoulder.boot.process.utils.FormUtils;
import com.shoulder.boot.process.vo.FormVo;
import com.shoulder.boot.process.dto.FormModDTO;
import com.shoulder.boot.process.dto.FormQueryDTO;
import com.shoulder.boot.process.entity.Form;
import com.shoulder.boot.process.mapper.FormMapper;
import com.shoulder.boot.process.service.FormService;
import com.shoulder.boot.process.exception.ProcessException;
import com.shoulder.core.fetch.BaseIds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.Collections;

/**
 * 表单信息 服务实现类
 *
 * @author 596767880@qq.com
 * @since 2025-09-05 21:38:08
 */
@Service
@Transactional
public class FormServiceImpl extends ServiceImpl<FormMapper, Form> implements FormService {


    @Override
    public List<FormVo> listQuery(FormQueryDTO queryDTO) {
        List<Form> list = this.baseMapper.selectList(FormUtils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return FormUtils.buildUtils().toVos(list);
    }

    @Override
    public FormVo look(String id) {
        Form entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new ProcessException("表单信息不存在");
        }
        return FormUtils.buildUtils().toVo(entity);
    }

    @Override
    public List<FormVo> looks(BaseIds<String> baseIds) {
        List<Form> list = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return FormUtils.buildUtils().toVos(list);
    }

    @Override
    public void del(String id) {
        Form entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new ProcessException("表单信息不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<FormVo> pageQuery(FormQueryDTO queryDTO) {
        Page<Form> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
                , FormUtils.buildQueryWrapper(queryDTO));
        if (selectPage == null || CollectionUtils.isEmpty(selectPage.getRecords())) {
            return new Page<>();
        }
        return FormUtils.buildUtils().toPage(selectPage);
    }

    @Override
    public void mod(FormModDTO modDTO) {
        Form info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new ProcessException("表单信息不存在"));
        String tenantId = TokenUtils.parseUser().getTenantId();
        Form exist = this.baseMapper.selectOne(new LambdaQueryWrapper<Form>()
                .ne(Form::getId, info.getId()).eq(Form::getName, modDTO.getName()).eq(Form::getTenantId, tenantId));
        if (!ObjectUtils.isEmpty(exist)) {
            throw new ProcessException("表单数据已存在");
        }
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(FormAddDTO addDTO) {
        String tenantId = TokenUtils.parseUser().getTenantId();
        Form info = this.baseMapper.selectOne(new LambdaQueryWrapper<Form>().eq(Form::getName, addDTO.getName()).eq(Form::getTenantId, tenantId));
        if (!ObjectUtils.isEmpty(info)) {
            throw new ProcessException("表单数据已存在");
        }
        info = new Form();
        info.setTenantId(tenantId);
        BeanUtils.copyProperties(addDTO, info);
        this.baseMapper.insert(info);
    }
}
